<?php
namespace Mob\Model;

use Think\Model;

/**字段数据模型，通过该模型可以方便地对字段信息实现读和取
 * Class DataModel
 */
class OrgaModel extends Model
{

    public function get_OrgaAll($schooId = '10183')
    {
     $where = array('schoolId'=>$schooId,'status'=>1);
     $orgaList = $this->where($where)->order('sortId,superSortId,orgaId,orgaType')->field('orgaId,orgaName,orgaType,desc,superId,sortId,superSortId')->select();
     return $orgaList;


    }
    public function set_OrgaTeacher($orgaId,$teacherId,$schooId = '10183')
    {
    $where = array('orgaId'=>$orgaId,'status'=>1);
   return $this->where($where)->save(array('teacherId'=>$teacherId));

    }
    public function get_OrgaNextNodeId($nodeType,$currentId=0,$schooId = '10183')
    {
    $where = array('orgaType'=>$nodeType,'schooId'=>$schooId);
    $res = $this->where($where)->field('max(orgaId) as orgaId')->find()['orgaId'];
    if(!$res)
    {
    $res=1;
    }else
    {
    $res++;
    }
    return $res;

    }
    public function add_OrgaNode($orgaId,$superId,$orgaName,$orgaType,$schooId,$desc,$sortId,$superSortId,$creatorId)
    {
    $data['orgaId']=$orgaId;
    $data['orgaName']=$orgaName;
    $data['superId']=$superId;
    $data['orgaType']=$orgaType;
    $data['status']=1;
    $data['schooId']=$schooId;
    $data['desc']=$desc;
    $data['sortId']=empty($sortId)?$orgaId:$sortId;
    $data['superSortId']=$superSortId;
    $data['teacherId']=$creatorId;
    $data['creatorId']=$creatorId;
   return $this->add($data);

    }
    public function del_OrgaNodeStatus($orgaId,$orgaType,$schooId,$status)
    {
    return $this->where(array('orgaId'=>$orgaId,'orgaType'=>$orgaType,'schooId'=>$schooId))->save(array('status'=>2));

    }
    public function is_ExistOrga($orgaId,$orgaType,$schooId = '10183')
    {
    $where = array('orgaId'=>$orgaId,'orgaType'=>$orgaType,'schooId'=>$schooId,'status'=>1);
    return $this->where($where)->field('orgaId,orgaName,orgaType,superId,schooId,teacherId,creatorId,teacherId')->find();


    }
    public function get_OrgaInfo($orgaid,$type=3,$schooId='10183')
    {
    	$where = array('schooId'=>$schooId,'orgaId'=>$orgaid,'orgaType'=>$type,'status'=>1);
    return   $this->where($where)->field('orgaId,orgaName,superId,desc,leaveApprovalUid,teacherId,superId')->find();

    }
    public function get_OrgaFullInfo($orgaid,$type=3,$schooId='10183')
    {
      $orga = new \ Think \ Model();
     $res = $orga->query("select t1.orgaId as collegeId ,t1.orgaName as collegeName,t2.orgaId as majorId,t2.orgaName as majorName,t3.orgaName as ClassName,t3.orgaId as ClassId  from ocenter_orga t1,ocenter_orga
t2,(select orgaName,orgaId,superId from ocenter_orga  where schooId = $schooId  and orgaId = '$orgaid' and status = 1) t3 where t2.orgaId = t3.superId and t1.orgaId = t2.superId and substr(t3.orgaId,1,2) = t1.orgaId limit 1")[0];
     return $res;

    }
   public function get_TeacherOrga($teacherId,$orgaType='2',$schooId='10183')
   {
   	$orga = new \ Think \ Model();
   	return $orga->query("select orgaId,schooId from ocenter_orga where (teacherId = $teacherId or leaveApprovalUid = $teacherId) and schooId = '$schooId'and status =1 and orgaType = $orgaType ")[0];

   }
  public function is_AuthVerify($teacherId,$orgaId,$orgaType,$schooId)
  {
   $orga = new \ Think \ Model();
   return $orga->query("select orgaId,schooId from ocenter_orga where  orgaId = $orgaId and status =1  and schooId = '$schooId'  and orgaType = $orgaType and  (teacherId = $teacherId or leaveApprovalUid = $teacherId)  ");
  }





}